package au.com.uptick.serendipity.client.presenter;

import au.com.uptick.serendipity.client.NameTokens;
import au.com.uptick.serendipity.client.view.SignInPageUiHandlers;
import au.com.uptick.serendipity.shared.FieldVerifier;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Button;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.*;

public class SignInPagePresenter extends Presenter<SignInPagePresenter.MyView, SignInPagePresenter.MyProxy> implements SignInPageUiHandlers {

    private final PlaceManager placeManager;
    private final ErrorDialogPresenterWidget errorDialog;

    @ProxyStandard
    @NameToken(NameTokens.signInPage)
    public interface MyProxy extends Proxy<SignInPagePresenter>, Place {
    }

    public interface MyView extends View, HasUiHandlers<SignInPageUiHandlers> {
        String getUserName();

        String getPassword();

        Button getSignInButton();

        void resetAndFocus();
    }

    @Inject
    public SignInPagePresenter(EventBus eventBus, MyView view, MyProxy proxy, PlaceManager placeManager, ErrorDialogPresenterWidget errorDialog) {
        super(eventBus, view, proxy);

        this.placeManager = placeManager;
        this.errorDialog = errorDialog;

        getView().setUiHandlers(this);
    }

    @Override
    protected void onBind() {
        super.onBind();

        // register Handler's
        registerHandler(getView().getSignInButton().addClickHandler(
                new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        sendCredentialsToServer();
                    }
                }));
    }

    @Override
    protected void onReset() {
        super.onReset();
        getView().resetAndFocus();
    }

    @Override
    protected void revealInParent() {
        // RevealRootLayoutContentEvent.fire(this, this);
        RevealRootContentEvent.fire(this, this);
    }

    private void sendCredentialsToServer() {
        String userName = getView().getUserName();
        String password = getView().getPassword();

        if (FieldVerifier.isValidUserName(userName) && (FieldVerifier.isValidPassword(password))) {
            PlaceRequest myRequest = new PlaceRequest(NameTokens.mainSmartGwtPage);

            // If needed, add URL parameters in this way:
            // myRequest = myRequest.with( "key1", "param1" ).with( "key2", "param2" );
            placeManager.revealPlace(myRequest);
        } else {
            showErrorDialog();
        }
    }

    @Override
    public void showErrorDialog() {
        addToPopupSlot(errorDialog);
    }
}
